from string import atof
from struct import pack
import sys

FILENAME="message_template.msg"
def hash(name):
    """compute a hash"""
    hash = 0
    for c in name:
        hash = hash + ord(c)
        hash = hash << 2
    return hash %8192

    hash=0
    for c in name:
        hash = (hash + ord(c))*2
    hash = hash * 2
    hash = hash & 0x1fff
    return hash

class Parser:
    def __init__(self,filename):
        self.fp = open(filename,"r")


    def nextLine(self):
        """return the next line"""
        while 1:
            line = self.fp.readline()
            if not line: return None

            line=line.strip()
            if line.startswith("//") or len(line)==0:
                continue

            # we found a valid line, return the list of words
            #print "*",line
            return [word.strip() for word in line.split()]

    def parseVersion(self):
        """parse the version string which comes first"""
        i=0
        while 1:
            words=self.nextLine()
            if words[0]=="version":
                self.version = atof(words[1])
                return
            elif words[0]=="{":
                raise "No Version found"
            i=i+1
            #if i<20: sys.exit(0)

    def searchFor(self,s):
        """search for a string"""
        while 1:
            words = self.nextLine()
            if words[0]==s:
                return True

    def parseBlocks(self):
        """Parse the blocks"""
        blocks=[]
        while 1:
            words = self.nextLine()
            #print words
            if words[0]=="}":
                return blocks
            elif words[0]=="{": # start of a block
                words = self.nextLine()
                name = words[0]
                typ = words[1]
                if typ=="Multiple":
                    multiplier = words[2]
                else:
                    multiplier = None
                print "  block",name 
                self.parseFields()

    def parseFields(self):
        """parse the fields in a block"""
        fields=[]
        while 1:
            words = self.nextLine()
            if words[0]=="}":
                return fields
            elif words[0]=="{":
                # a field line
                name = words[1]
                typ = words[2]
                if typ == "Variable":
                    typ2 = words[3]
                else:
                    typ2 = None
                print "    field: ",name,typ,hash(name)

    def parseTemplate(self):
        """parse a template"""
        while 1:
            words = self.nextLine()
            if words is None: break
            if words[0]=="{": # it's a start!
                words = self.nextLine() # name and options
                name = words[0]
                klass = words[1]
                trusted = words[2]
                zerocoded = words[3]
                print "template: ",name,klass

                # and now for some blocks
                self.parseBlocks()
        return False

    def parse(self):
        """Parse the funky message template"""
        self.parseVersion()
       
        running = True
        while running:
            running = self.parseTemplate()
                

            

            

if __name__=="__main__":
   p=Parser(FILENAME)
   p.parse()
